﻿using ZeroSum.App.Extensions;
using ZeroSum.App.Middlewares;
using ZeroSum.DependencyInjection.Attributes;
using ZeroSum.DependencyInjection.Interfaces;
using ZeroSum.Extend.EFCore.MySql;
using ZeroSum.Extend.Mapper;
#if (EnableSwagger)
using ZeroSum.Extend.Swagger;
#endif
#if (EnableCors)
using ZeroSum.Extend.Cors;
#endif
namespace ZeroSumTemplate.Api;

/// <summary>
///     Identity层初始化注册，要早于AspNetCore
///     晚于Domain, Infrastructure, Application
/// </summary>
[StartupOrder(1001)]
public class Startup : IAppStartup
{
    public void ConfigurationServices(IServiceCollection services, IConfiguration configuration)
    {
        // 注册授权相关服务的实现

        services.AddDistributedMemoryCache();
        services.AddAutoMapper();
        services.AddHttpContextAccessor();
        services.AddControllers().AddFriendlyResponse();
#if (EnableCors)
        services.AddCorsSupported(configuration);
#endif

        services.AddEFCore(configuration);
#if EnableSwagger
        services.AddSwaggerDoc(configuration);
#endif

    }

    public void Configuration(IApplicationBuilder app)
    {
        // 全局异常处理
        app.EnableExceptionHandler();

        // 自动提交数据库改动
        app.EnableAutoSave();
#if (EnableCors)
        app.UseCorsSupported();
#endif

#if EnableSwagger
        app.UseSwaggerDoc();
#endif
        app.UseAutoMapper();
        app.UseRouting();

        // app.UseAuthorization();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllers();
        });
    }
}